package com.example.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.commons.annotation.DebounceLock;
import com.example.commons.annotation.SpendTime;
import com.example.commons.response.ResponseResult;
import com.example.commons.response.RestResultUtil;
import com.example.commons.util.*;
import com.example.commons.vo.UserVo;
import com.example.user.config.Wx;
import com.example.user.config.WxPayConfig;
import com.example.user.entity.po.Order;
import com.example.user.service.UsersService;
import com.example.user.service.feign.UserAccountClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Author:汪彬彬
 * date:2024/11/5 13:08
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class TestController {

    @Autowired
    UserAccountClient userAccountClient;

    @Autowired
    UsersService usersService;

    @Autowired
    WxPayConfig wxPayConfig;


    static String prefix = "cart:user:";

    @PostMapping("/query")
    public ResponseResult query() {
        Long id = UserUtil.get().getId();
        return RestResultUtil.success(userAccountClient.test(id + "").getData());
    }

    @SpendTime
    @PostMapping("/test")
    @DebounceLock(prefix = "queryUser", expire = 2, unit = TimeUnit.SECONDS)
    public ResponseResult query(@RequestBody UserVo userVo) {
        Wx bean = SpringContextHolder.getBean(Wx.class);
        System.out.println(bean.getAge());
        return RestResultUtil.success(usersService.query(userVo));
    }


    @PostMapping("/add")
    public void query1(@RequestBody Order order) throws IOException {
        Long userId = UserUtil.get().getId();
        String key = prefix + userId;
        if (RedisUtils.HashOps.hExists(key, String.valueOf(order.getProductNo()))) {
            String obj = (String) RedisUtils.HashOps.hGet(key, String.valueOf(order.getProductNo()));
            Order order1 = JSONObject.toJavaObject(JSONObject.parseObject(obj), Order.class);
            order1.setNum(order1.getNum() + order.getNum());
            RedisUtils.HashOps.hPut(key, String.valueOf(order.getProductNo()), BeanUtil.bean2JsonString(order1), 30, TimeUnit.MINUTES);
        } else {
            RedisUtils.HashOps.hPut(key, String.valueOf(order.getProductNo()), BeanUtil.bean2JsonString(order), 30, TimeUnit.MINUTES);
        }

    }

    @PostMapping("/del")
    public ResponseResult del(@RequestBody Order order) throws IOException {
        Long userId = UserUtil.get().getId();
        String key = prefix + userId;
        if (RedisUtils.HashOps.hExists(key, String.valueOf(order.getProductNo()))) {
          List<String> list = new ArrayList<>();
          list.add(String.valueOf(order.getProductNo()));
          RedisUtils.HashOps.hDelete(key,list);
        } else {
            throw new RemoteException("商品不存在");
        }
        return RestResultUtil.success("操作成功");
    }

    @PostMapping("/queryP")
    @SpendTime
    public ResponseResult query(@RequestBody Order order) throws IOException {
        Long userId = UserUtil.get().getId();
        String key = prefix + userId;
        if (order.getProductNo() != null) {
            Order order1 = BeanUtil.obj2Bean((String) RedisUtils.HashOps.hGet(key, String.valueOf(order.getProductNo())), Order.class);
            AssertUtil.isNull(order1,"没有查询到商品");
            return RestResultUtil.success(BeanUtil.bean2Json(order1));
        } else {
            List<Order> list = new ArrayList<>();
            Map<Object, Object> map = RedisUtils.HashOps.hGetAll(key);
            for (Object key1 : map.keySet()) {
                list.add(BeanUtil.obj2Bean((String) map.get(key1), Order.class));
            }
            return RestResultUtil.success(list);
        }
    }


}
